﻿<Window x:Class="WPFStorageClient.Window1" x:Name="_window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sc="clr-namespace:WPFStorageClient"
    Title="Azure Storage Client" Height="422" Width="800" FontSize="18" Closed="_window1_Closed">
    <Window.Resources>
        <ContextMenu x:Key="ContainerContextMenu">
            <MenuItem Header="Upload new file" Click="UpdloadNewFile_Click"/>
            <MenuItem  Header="Delete" Click="DeleteContainer_Click"/>
        </ContextMenu>
        <ContextMenu x:Key="TreeViewContextMenu">
            <MenuItem Header="Create Container" Click="CreateContainer_Click"/>
        </ContextMenu>
        <ContextMenu x:Key="ListBoxBlobContextMenu">
            <MenuItem Header="Delete Blob" Click="DeleteBlob_Click"/>
            <MenuItem Header="Copy Url to clipboard" Click="GetUriForBlob_Click"/>
        </ContextMenu>
        <HierarchicalDataTemplate DataType="{x:Type sc:Container}" 
                                  ItemsSource="{Binding Prefixes}">
            <TextBlock Text="{Binding ContainerName}" ContextMenu="{StaticResource ContainerContextMenu}"/>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type sc:Blob}">
            <TextBlock Text="{Binding BlobName}" />
        </HierarchicalDataTemplate>
        <Style x:Key="{x:Type ProgressBar}"
            TargetType="{x:Type ProgressBar}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ProgressBar}">
                        <Grid MinHeight="14" MinWidth="200">
                            <Border 
              Name="PART_Track" 
              CornerRadius="2" 
              Background="Gray"
              BorderThickness="1" />
                            <Border 
              Name="PART_Indicator" 
              CornerRadius="2" 
              Background="Green"
              BorderBrush="Aqua"
              BorderThickness="1"
              HorizontalAlignment="Left" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <HierarchicalDataTemplate x:Key="UploadItemTextTemplate" DataType="{x:Type sc:UploadItem}">
            <DockPanel LastChildFill="True">
                <ProgressBar Width="200" DockPanel.Dock="Right" Value="{Binding PercentComplete}" Maximum="100" />
                <TextBlock Width="300" Text="{Binding FileName}" DockPanel.Dock="Right"/>
            </DockPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate x:Key="AccountInfoTemplate">
            <DockPanel LastChildFill="True" Margin="5">
                <TextBlock Width="300" Text="{Binding Path=Attribute[accountnamevalue].Value}"  DockPanel.Dock="Left"/>
                <TextBlock Text="{Binding Path=Attribute[accountsharedkey].Value}"  DockPanel.Dock="Left"/>                
            </DockPanel>
        </HierarchicalDataTemplate>
        
        <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
            <Setter Property="Margin" Value="5"/>
            <Setter Property="ContextMenu" Value="{StaticResource ListBoxBlobContextMenu}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding  ElementName=_listBox, Path=SelectedIndex}" Value="-1">
                    <Setter Property="ContextMenu" Value="{x:Null}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        
    </Window.Resources>
    <Grid>
        <TabControl>
            <TabItem Header="Browser">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="auto" MinWidth="2" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" MinHeight="44" />
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto" MinHeight="30" />
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Row="2" Grid.ColumnSpan="3">
                        <TextBox Name="_statusText" Text="{Binding StatusText}"></TextBox>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.ColumnSpan="3" DataContext="{Binding StorageAccounts}">
                        <StackPanel.Resources>
                            <!--
                <Style x:Key="ConnectButtonStyle" TargetType="{x:Type Button}">
                    <Setter Property="IsEnabled" Value="True"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=SelectedIndex, ElementName=_comboBox}" Value="-1">
                            <Setter Property="IsEnabled" Value="False"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
                -->
                        </StackPanel.Resources>
                        <Label Content="Azure storage" Margin="5"/>
                        <ComboBox  x:Name="_accountSelectionComboBox" AllowDrop="True" Width="200" Margin="5"
                            ItemsSource="{Binding Path=Elements}" SelectionChanged="Account_SelectionChanged">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=Attribute[accountnamevalue].Value}"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                    </StackPanel>
                    <GridSplitter Grid.Column="1" Grid.Row="1" Width="2"/>
                    <TreeView x:Name="_treeView" Grid.Row="1" Margin="5,1,5,9" 
                          BorderBrush="DarkGray" ItemsSource="{Binding Containers}"
                          ContextMenu="{StaticResource TreeViewContextMenu}" 
                          SelectedItemChanged="TreeView_SelectedItemChanged"/>
                    <ListBox Style="{StaticResource ListBoxStyle}"
                         Name="_listBox" Grid.Row="1" Grid.Column="2" 
                         ItemsSource="{Binding ElementName=_treeView, Path=SelectedItem.Blobs}"
                         BorderBrush="DarkGray" Margin="5,1,5,9">
                    </ListBox>
                </Grid>
            </TabItem>
            <TabItem Header="Upload Progress" Visibility="Visible" >
                <!-- Figure out why this trigger is not working... -->
                <TabItem.Style>
                    <Style TargetType="TabItem">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=UploadItems.Count}" Value="0">
                                <Setter Property="Visibility" Value="Hidden" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TabItem.Style>

                <ListBox ItemsSource="{Binding UploadItems}" ItemTemplate="{StaticResource UploadItemTextTemplate}"/>
            </TabItem>
            <TabItem DataContext="{Binding StorageAccounts}" Header="Account Management">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="auto"/>
                    </Grid.RowDefinitions>
                    <DataGrid x:Name="_dataGrid" ItemsSource="{Binding Path=Elements}" Grid.Row="0" SelectionChanged="_dataGrid_SelectionChanged">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding Path=Attribute[accountnamevalue].Value}" Header="Account Name"/>
                            <DataGridTextColumn Binding="{Binding Path=Attribute[accountsharedkey].Value}" Header="Account Share Key"
                                                MaxWidth="500">
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                    <!-- TODO - Figure out why my buttons are not aligning to the right -->
                    <DockPanel Grid.Row="1"  LastChildFill="false" >
                        <Button Margin="5" Content="New" Width="75" DockPanel.Dock="Right" Click="NewAccount_Click"/>
                        <Button Margin="5" Content="Edit" Width="75"
                                IsEnabled="{Binding ElementName=_window1, Path=EnableAccountEdit}" DockPanel.Dock="Right" Click="EditButton_Click" />
                        <Button Margin="5" Content="Delete" Width="75"
                                IsEnabled="{Binding ElementName=_window1, Path=EnableAccountEdit}" DockPanel.Dock="Right" Click="DeleteAccountButton_Click" />
                    </DockPanel>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>
